package redPacket;

import java.util.ArrayList;

/**
 * @description: ToDo
 * @author: marin
 * @time: 2020/7/3 9:36
 */
public class RandomPacket implements OpenMode {

    @Override
    public ArrayList<Integer> divide(int total, int count) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        int num = total/count;
        int min = 1 ;
        int[] result = new int[count];
        result = getRandomAndTotalEq(count,total,min,result);
        for (int i=0;i<result.length;i++){
            list.add(result[i]);
        }
        return list;
    }

    //递归调用
    public static int[] getRandomAndTotalEq(int count,int total,int min,int[] result){
        int random = 0 ;
        if(count>1){
            int useTotal = total-(count-1)*min;
            random =(int)(Math.random()*(useTotal-1)+1);
        }else{
            random = total;
        }
        result[count-1] = random;
        int surplusTotal = total-random;
        count--;
        if(count>0){
            getRandomAndTotalEq(count,surplusTotal,min,result);
        }
        return result;
    }

}
